package us.mooresr.webtroopmanager.client.ui;

import us.mooresr.webtroopmanager.client.place.LoginPlace;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class RegistrationViewIpml extends Composite implements RegistrationView {
	private Presenter presenter;

	private static RegistrationViewIpmlUiBinder uiBinder = GWT
			.create(RegistrationViewIpmlUiBinder.class);

	interface RegistrationViewIpmlUiBinder extends
			UiBinder<Widget, RegistrationViewIpml> {
	}
	
	@UiField
	TextBox usernameTextBox;
	
	@UiField
	TextBox realNameTextBox;
	
	@UiField
	PasswordTextBox passwordTextBox;
	
	@UiField
	PasswordTextBox confirmPasswordTextBox;
	
	@UiField
	TextBox emailAddressTextBox;
	
	@UiField
	Button okButton;
	
	@UiField
	Button cancelButton;
	
	@UiHandler("okButton")
	void onOkButton(ClickEvent e) {
		// Validate
		
		// Blank user name?
		if (usernameTextBox.getText().length() < 1) {
			Window.alert("Username is required.");
			return;
		}
		
		// Duplicate user name?
		/*
		if (the name already exists) {
			Window.alert("Username already in use, please choose a different one.");
			return;
		}
		*/
		
		// Blank real name?
		if (realNameTextBox.getText().length() < 3) {
			Window.alert("Please enter your real name.  Without this the administrator won't know whether or not to approve your request.");
			return;
		}
		
		// Blank password?
		if (passwordTextBox.getText().length() < 1) {
			Window.alert("Password is required.");
			return;
		}
		
		// Passwords don't match?
		if (passwordTextBox.getText().equals(confirmPasswordTextBox.getText()) == false) {
			Window.alert("The passwords don't match.  Please type the same password in both fields.");
			return;
		}
		
		// email address blank?
		if (emailAddressTextBox.getText().length() < 3) {
			Window.alert("A valid email address is required.  This will be used to send you a confirmation of your registration.");
			return;
		}
		
		// No @ in the email address?
		if (emailAddressTextBox.getText().contains("@") == false) {
			Window.alert("A valid email address is required.  This will be used to send you a confirmation of your registration.");
			return;			
		}
		
		// Looks OK
		presenter.registerNewUser(usernameTextBox.getText(), passwordTextBox.getText(),
				realNameTextBox.getText(), emailAddressTextBox.getText());
		presenter.goTo(new LoginPlace());
		 
		
		
	}
	
	@UiHandler("cancelButton")
	void onCancelButton(ClickEvent e) {
		presenter.goTo(new LoginPlace());
	}
	
	

	public RegistrationViewIpml() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;		
	}

	@Override
	public void clearForm() {
		usernameTextBox.setValue("");
		passwordTextBox.setValue("");
		confirmPasswordTextBox.setValue("");
		realNameTextBox.setValue("");
		emailAddressTextBox.setValue("");
		
	}

}
